<?php

namespace app\base\controller\api;

use think\admin\Controller;
use app\base\model\AiDevice;
use app\base\controller\api\Auth;
use app\base\model\AiVoice;


/**
 * 设备相关
 * Class Login
 * @package app\store\controller
 */
class Device extends Auth
{
    public function getList()
    {
        $list = AiDevice::mQuery()->where(['user_id'=>$this->user_id])
            ->like('device_name')
            ->equal('group_id,status,work_status')->order('id desc')->select()->toArray();
        $this->success('获取成功', $list);
    }

    public function getDeviceListGroup()
    {
        $status = input('status',0);
        $where = ['user_id'=>$this->user_id];
        if($status){
            $where['status'] = 1;
            $where['work_status'] = 1;
            if(AiDevice::mQuery()->where($where)->count()==0){
                $this->error('没有在线或空闲设备');
            }

        }

        $list = $this->app->db->name("ai_device_group")->field('id as value,name as label')
            ->where(['user_id'=>$this->user_id])->order('id desc')->select()->toArray();
        foreach ($list as $k=>$item){
            $devices = AiDevice::mQuery()->field('id as value,device_name as label,device_bname')
                ->where($where)
                ->where(['group_id'=>$item['value']])->select()->toArray();
            foreach ($devices as &$device){
                if($device['device_bname']){
                    $device['label'] = '('.$device['device_bname'].')'.$device['label'];
                }

            }
            if(empty($devices)){
                unset($list[$k]);
            }else{
                $list[$k]['children'] = $devices;
            }

        }
        $this->success('获取成功', array_values($list));

    }

    //状态更新
    public function updateStatus()
    {
        $data = $this->request->post();
        $this->_vali([
            'id.require' => '设备ID不能为空!',
        ],$data);
        $res = AiDevice::mUpdate($data);
        if($res){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
    }

    //保存订单数据
    public function update()
    {
        $data = $this->request->post();
        $this->_vali([
            'device_bname.require' => '设备备注不能为空!',
            'id.require' => '设备ID不能为空!',
        ],$data);
        $res = AiDevice::mSave($data);
        if($res){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }
    //删除设备
    public function deleteDevice()
    {
        $data = $this->request->get();
        $this->_vali([
            'id.require' => '设备ID不能为空!',
        ],$data);
        $res = $this->app->db->name("ai_device")->where(['id'=>$data['id'],'user_id'=>$this->user_id])->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    public function getGroupList()
    {
        $list = $this->app->db->name("ai_device_group")->where(['user_id'=>$this->user_id])->select()->toArray();
        $this->success('获取成功', $list);

    }
    public function saveGroup()
    {
        $data = $this->request->post();
        $this->_vali([
            'name.require' => '分组名称不能为空!',
        ],$data);
        $data['user_id'] = $this->user_id;
        $res = $this->app->db->name("ai_device_group")->insert($data);
        if($res){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }

    }


}
